package com.jy.rhin.domain.document.filter.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jy.rhin.domain.document.filter.DocumentFilter;
import com.jy.rhin.domain.document.filter.DocumentFilterDescEnum;
import com.jy.rhin.domain.exception.RhinInfraErrorType;
import com.jy.rhin.infra.repo.document.submission.mapper.DocumentSubmissionSetMapper;
import com.jy.rhin.infra.repo.document.submission.po.DocumentSubmissionSetPO;
import com.jy.rhin.infra.uitl.Asserts;
import com.jy.rhin.sdk.dto.document.DocumentContentDto;
import com.jy.rhin.sdk.dto.document.DocumentSubmissionSetDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author WeiQ
 * @date 2022年6月8日 16:37:19
 * @Description 文档提交集 唯一性校验
 */
@Slf4j
@Service
public class DocumentFilter4SubmissionSetUniqueness implements DocumentFilter {

    @Resource
    DocumentSubmissionSetMapper documentSubmissionSetMapper;

    @Override
    public boolean shouldFilter(DocumentSubmissionSetDto submissionSet, List<DocumentContentDto> contentList) {
        return true;
    }

    /**
     * 文档提交集  uniqueId必须唯一
     *
     * @param submissionSet
     * @param contentList
     */
    @Override
    public void doFilter(DocumentSubmissionSetDto submissionSet, List<DocumentContentDto> contentList) {
        QueryWrapper<DocumentSubmissionSetPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(DocumentSubmissionSetPO::getUniqueId, submissionSet.getUniqueId());
        Integer count = documentSubmissionSetMapper.selectCount(queryWrapper);
        Asserts.equals(0, count, RhinInfraErrorType.SUBMISSION_SET_ID_EXISTS);
    }

    @Override
    public DocumentFilterDescEnum filterDesc() {
        return DocumentFilterDescEnum.SUBMISSION_SET_UNIQUENESS;
    }
}
